﻿using App.Chat.Application.Interface;
using App.Chat.Auth;
using App.Chat.Contract;
using App.Chat.Contract.Input;
using App.Chat.Contract.Ouput;
using App.Chat.Models.Models;
using Microsoft.AspNetCore.Mvc;

namespace App.Chat.Controllers
{
    [Route("api/[controller]")]
    [NeedAuth]
    [ApiController]
    public class FriendController : BaseController
    {
        private readonly IFriendService _friendService;
        private readonly IUserService _userService;
        public FriendController(CurrentUser currentUser,
            IFriendService friendService,
            IUserService userService) : base(currentUser)
        {
            _friendService = friendService;
            _userService = userService;
        }
        /// <summary>
        /// 添加好友
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("addFriendAsk")]
        public async Task<string> AddFriendAskAsync(FriendAskInput input)
        {
            await _friendService.AddFriendAskAsync(new FriendAsk
            {
                AskUserId = input.AskUserId,
                FromId = _currentUser.Id,
                Remark = input.AskRemark
            });
            return "好友请求发送成功";
        }

        [HttpPost("decideFriendAsk")]
        public async Task<string> DecideFriendAskAsync(FriendAskDecideInput input)
        {
            try
            {
                await _friendService.DecideFriendAskAsync(_currentUser, input.AskId, input.GetIsAgree());
                return "success";
            }
            catch (Exception ex)
            {
                return "error";
            }
        }
        /// <summary>
        /// 好友请求列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("FriendAskList")]
        public async Task<List<FriendAskOutput>> FriendAskListAsync()
        {
            var outputList = new List<FriendAskOutput>();
            var friendAskList = await _friendService.FriendAskListAsync(_currentUser.Id);
            if (friendAskList.Count == 0)
                return outputList;
            var userIds = friendAskList.Select(x => x.FromId).ToArray();
            var userList = await _userService.FindUsersByIdsAsync(userIds);
            friendAskList.ForEach(x =>
            {
                outputList.Add(new FriendAskOutput
                {
                    Id = x.Id,
                    IsAgree = x.IsAgree,
                    UserName = userList.Where(u => u.Id.Equals(x.FromId)).FirstOrDefault()!.UserName
                });
            });
            return outputList;
        }

        /// <summary>
        /// 好友列表，跟会话无关
        /// </summary>
        /// <returns></returns>
        [HttpGet("FriendList")]
        public async Task<List<FriendOutput>> FriendListAsync()
        {
            var outputList = new List<FriendOutput>();
            var friends = await _friendService.FindFriendListAsync(_currentUser.Id);
            if (friends != null && friends.Count == 0)
                return outputList;
            var users = await _userService.FindUsersByIdsAsync(friends.Select(x => x.FriendId).ToArray());
            friends.ForEach(x =>
            {
                outputList.Add(new FriendOutput
                {
                    Id = x.FriendId,
                    Name = x.FriendRemark ?? users.Where(u => u.Id.Equals(x.FriendId)).First().UserName
                });
            });
            return outputList;
        }
        /// <summary>
        /// 修改备注
        /// </summary>
        /// <returns></returns>
        [HttpPost("ChangeRemark")]
        public async Task<string> ChangeRemarkAsync(FriendRemarkInput input)
        {
            await _friendService.ChangeRemarkAsync(_currentUser.Id, input.Id, input.FriendRemark);
            return "修改成功";
        }
        [HttpGet("DeleteFriend")]
        public async Task<string> DeleteFriendAsync(string friendUserId)
        {
            await _friendService.DeleteFriendAsync(_currentUser.Id, friendUserId);
            return "删除成功";
        }
    }
}
